﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ExportMaintainRecords : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropDownList();
            }
        }

        private void BindDropDownList()
        {
            UserService userservice = new UserService();
            List<WXYW.DAL.User> engineers = userservice.GetUsersByRoleType((int)RoleType.Engineer);
            List<WXYW.DAL.User> platformEngineers = userservice.GetUsersByRoleType((int)RoleType.PlatformEngineer);
            engineers.AddRange(platformEngineers);
            engineers.Insert(0, new WXYW.DAL.User() { ContactPerson = "所有", UserID = 0 });
            engineerDropDownList.DataSource = engineers;
            engineerDropDownList.DataTextField = "ContactPerson";
            engineerDropDownList.DataValueField = "UserID";
            engineerDropDownList.DataBind();

            ServiceStationService ssService = new ServiceStationService();
            List<ServiceStation> stations = ssService.GetALLServiceStations().ToList();
            stations.Insert(0, new ServiceStation() { Name = "所有", ID = 0 });
            serviceStationDropDownList.DataSource = stations;
            serviceStationDropDownList.DataTextField = "Name";
            serviceStationDropDownList.DataValueField = "ID";
            serviceStationDropDownList.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ExportService service = new ExportService();           
           
            string filename = "维修产品";
            if (!string.IsNullOrEmpty(formTypeDropDownList.SelectedValue))
            {
                switch (formTypeDropDownList.SelectedValue)
                {
                    case "R":
                        filename = "RMA" + filename;
                        break;
                    case "D":
                        filename = "DOA" + filename;
                        break;
                    case "B":
                        filename = "备件先行" + filename;
                        break;
                    case "C":
                        filename = "库存清理" + filename;
                        break;
                }
            }
            if (!formTypeDropDownList.SelectedValue.Equals("B"))
            {
                GridView.DataSource = service.GetMaintainProductModel(startTimeTextBox.Text, endTimeTextBox.Text,
               engineerDropDownList.SelectedValue, serviceStationDropDownList.SelectedValue,
               typeDropDownList.SelectedValue, formTypeDropDownList.SelectedValue);
                GridView.DataBind();
                ExportExcel.ExportExcelFromGridView(GridView, filename);
                GridView.DataSource = null;
                GridView.DataBind();
            }
            else
            {
                BJXXGridView.DataSource = service.GetMaintainProductModel(startTimeTextBox.Text, endTimeTextBox.Text,
               engineerDropDownList.SelectedValue, serviceStationDropDownList.SelectedValue,
               typeDropDownList.SelectedValue, formTypeDropDownList.SelectedValue);
                BJXXGridView.DataBind();
                ExportExcel.ExportExcelFromGridView(BJXXGridView, filename);
                BJXXGridView.DataSource = null;
                BJXXGridView.DataBind();
            }            
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
    }
}
